# from re import A
import time
from flask import Blueprint, request, jsonify
from flask_restful import Api, Resource
from settings.mysqlM import db
from settings.dataM import get_data
from settings.jwtM import jwtm
from .acl import acl
from settings.redisM import r

logdp = Blueprint('log', __name__)
api = Api(logdp)


class Login(Resource):
    def post(self):
        data1 = get_data()
        start = time.time()
        username = data1['username']
        password = data1['password']

        sql = "select * from users where username='{}' and password='{}'".format(username, password)
        res = db.find(sql)
        id = res['id']
        name = res['name']
        if res:
            data={'data':{'id':id,'name':name,'iat':start,'exp':60}}               
            token = jwtm.jwt_en(data)
            # 获取type1的url
            page_lst = acl.get_page_reslist(res['id'])
            # 获取type2的url
            inter_lst = acl.get_interface_reslist(res['id'])
            key = 'interface' + str(res['id'])
            r.add_set(key, inter_lst)

            return jsonify({'code': 200, 'token': token, 'page_list': page_lst, 'data': res})
        else:
            return jsonify({'code': 40010, 'msg': '用户不存在'})


api.add_resource(Login, '/log')
